package net.root.plain.main;

import java.util.ArrayList;
import java.util.List;

import net.root.plain.alarm.AlarmDAO;
import net.root.plain.alarm.AlarmDTO;
import net.root.plain.util.DBConn;
import net.root.plain.util.ULog;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity {
	Button btn;
	ListView list;
	List<AlarmDTO> lists = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btn = (Button) findViewById(R.id.main_button1);
		btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(MainActivity.this, AddActivity.class);
				startActivity(intent);
			}
		});
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		AlarmDAO dao = new AlarmDAO(DBConn.getConnection(this));
		lists = dao.getLists();

		ArrayList<String> arGeneral = new ArrayList<String>();
		for (int i = 0; i < lists.size(); i++) {
			arGeneral.add(lists.get(i).getAlarmLabel());
		}

		ArrayAdapter<String> adapter;
		adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_multiple_choice, arGeneral);
		list = (ListView) findViewById(R.id.main_listView1);
		list.setAdapter(adapter);
		list.setOnItemClickListener(mItemClickListener);
		super.onResume();
	}

	OnItemClickListener mItemClickListener = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> arg0, View v, int position,
				long arg3) {
			// TODO Auto-generated method stub
			AlarmDTO dto = lists.get(position);
			ULog.showToast(MainActivity.this, dto.toString());
			Intent intent = new Intent(MainActivity.this, AddActivity.class);
			intent.putExtra(DBConn.columns[0], dto.getNo());
			startActivity(intent);
		}
	};

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
}